package com.bocek.chakmapoint.controller;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import javax.swing.JPanel;

import com.bocek.chakmapoint.model.Slide;
import com.bocek.chakmapoint.model.SlideItem;
import com.bocek.chakmapoint.model.SlideList;
import com.bocek.chakmapoint.view.MainView;
import com.bocek.chakmapoint.view.slideshow.SlideshowView;

/**
 * Controller for the slide show.
 * 
 * @author  Baris Sencan
 * @version 1.0
 */
public class SlideshowController extends KeyAdapter {

	private static SlideshowController instance = null;

	private JPanel jPanel = null;

	private Slide currentSlide;
	private int currentSlideNo;

	private SlideshowController() {
		currentSlideNo = 1;
		currentSlide = SlideList.getSlide(currentSlideNo);
	}

	public static SlideshowController getInstance() {
		if (instance == null) {
			instance = new SlideshowController();
		}

		return instance;
	}
	
	public Iterator<SlideItem> getItemIterator() {
		return currentSlide.getSlideItems().iterator();
	}
	
	public void setPanel(JPanel panel) {
		jPanel = panel;
	}
	
	public int getCurrentSlideNo() {
		return currentSlideNo;
	}

	public void activate() {
		currentSlideNo = 1;
		currentSlide = SlideList.getSlide(currentSlideNo);
		drawSlide();
	}

	public void drawSlide() {
		if (jPanel != null) jPanel.repaint();
	}

	public void nextSlide() {
		if (!(currentSlideNo >= SlideList.getSlides().size())) {
			currentSlide = SlideList.getSlide(++currentSlideNo);
			drawSlide();
		}
	}

	public void previousSlide() {
		if (currentSlideNo > 1) {
			currentSlide = SlideList.getSlide(--currentSlideNo);
			drawSlide();
		}
	}

	@Override
	public void keyPressed(KeyEvent evt) {
		if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
			SlideshowView.getInstance().setVisible(false);
			MainView.getInstance().setVisible(true);
		} else if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
			nextSlide();
		} else if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
			previousSlide();
		} else {
			System.out.println(evt.getKeyCode());
		}
	}
}